<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;

class RecruitImgController extends Controller
{

    public function getFocusMapList(){
        try{
            $results = DB::table('aboutcompany')->where('type','=','1')->get();
            //dd($results);

            return view('Admin.recruitimglist',['results'=> $results]);

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

    public function focusMapUpload(Request $request){

        if($request->isMethod('POST')){

            $file = $request->file('aboutcompany_image');
            if($file->isValid()){

                $mimetype = $file->getClientMimeType();
                $originalName = $file->getClientOriginalName();

                $ext = $file->getClientOriginalExtension();
                $realPath = $file->getRealPath();

                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;

                $bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));



                $path = "/../public/uploads/";
                $url = '/uploads/'.$filename;

                $result = DB::table('aboutcompany')->insert([
                    'aboutcompany_image'=> $url,'type' => 1
                ]);
                if($result){
                    return redirect('/admin/getFocusMapList');
                }
            }
            exit;
        }

    }

    public function  deleteFocusMap(Request $request){
        try{
            $aboutcompany_id = $request->input('aboutcompany_id');

            $results = DB::table('aboutcompany')->where('aboutcompany_id','=',$aboutcompany_id)->delete();
            if($results){

                return redirect('/admin/getFocusMapList');
            }

        }catch (Exception $e){

            $info =  $e->getMessage().'failed line:'.$e->getLine().'failed string:'.$e->getTraceAsString();
            return json_encode(['msg'=>0,'data' => $info]);
            exit;
        }
    }

}
